package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.StaffDTO;
import com.nit.lab.entity.basic.StaffEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface StaffMapper {

    @Select({"<script>SELECT " +
            "staff_id AS `key`, staff_id, staff_name, staff_sex, staff_image, staff_tel, " +
            "staff_birth, staff_info, bs.dept_id, bs.dept_father, dept_name, bs.gmt_create " +
            "FROM basic_staff bs, basic_dept bd " +
            "WHERE bs.dept_id = bd.dept_id " +
            "<if test='staffName != null'> AND staff_name LIKE CONCAT ('%', #{staffName}, '%') </if> " +
            "<if test='deptId != null'> AND bs.dept_id = #{deptId} </if> " +
            "ORDER BY " +
            "<if test='sortField != null'> " +
            "${sortField} ${sortOrder}, " +
            "</if> " +
            "staff_id DESC " +
            "LIMIT #{start}, #{length} " +
            "</script>"})
    List<StaffDTO> list4Table(@Param("start") Integer start,
                              @Param("length") Integer length,
                              @Param("staffName") String staffName,
                              @Param("deptId") String deptId,
                              @Param("sortField") String sortField,
                              @Param("sortOrder") String sortOrder) throws Exception;

    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_staff bs, basic_dept bd " +
            "WHERE bs.dept_id = bd.dept_id " +
            "<if test='staffName != null'> AND staff_name LIKE CONCAT ('%', #{staffName}, '%') </if> " +
            "<if test='deptId != null'> AND bs.dept_id = #{deptId} </if> " +
            "</script>"})
    Long count4Table(@Param("staffName") String staffName, @Param("deptId") String deptId) throws Exception;

    @Insert("INSERT INTO basic_staff (" +
            "staff_name, staff_sex, staff_tel, staff_birth, staff_info, dept_id, dept_father, gmt_create) " +
            "VALUES (#{entity.staffName}, #{entity.staffSex}, #{entity.staffTel}, #{entity.staffBirth}, " +
            "#{entity.staffInfo}, #{entity.deptId}, #{entity.deptFather}, #{entity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "staffId", keyColumn = "staff_id")
    Integer add(@Param("entity") StaffEntity staffEntity) throws Exception;

    @Update("UPDATE basic_staff " +
            "SET staff_name = #{entity.staffName},  " +
            "staff_sex = #{entity.staffSex}, " +
            "staff_tel = #{entity.staffTel}, " +
            "staff_birth = #{entity.staffBirth}, " +
            "staff_info = #{entity.staffInfo}, " +
            "dept_id = #{entity.deptId}, " +
            "dept_father = #{entity.deptFather}, " +
            "gmt_create = #{entity.gmtCreate} " +
            "WHERE staff_id = #{entity.staffId}")
    Integer update(@Param("entity") StaffEntity staffEntity) throws Exception;

    @Delete("DELETE FROM basic_staff WHERE staff_id = #{staffId}")
    Integer remove(@Param("staffId") Long staffId) throws Exception;

    @Select("SELECT staff_id AS `key`, staff_id, staff_name, staff_sex, staff_image, staff_tel, " +
            "staff_birth, staff_info, dept_id, dept_father, bs.gmt_create " +
            "FROM basic_staff WHERE staff_id = #{staffId} ")
    StaffDTO getStaffById(@Param("staffId") Long staffId) throws Exception;
}
